package com.chefu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chefu.common.exception.CfException;
import com.chefu.common.exception.ExceptionEnum;
import com.chefu.common.pojo.PageResult;
import com.chefu.common.utils.BeanHelper;
import com.chefu.system.mapper.DeptMapper;
import com.chefu.system.mapper.EmpMapper;
import com.chefu.system.pojo.Dept;
import com.chefu.system.pojo.Emp;
import com.chefu.system.service.EmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chefu.system.vo.EmpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author goodgoodstudy
 * @since 2021-03-30
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<EmpVo> findByPage(Integer page, Integer pageSize, String name, String phone, String email, Long deptId, Integer status) {
        IPage iPage = new Page(page, pageSize);

        QueryWrapper queryWrapper = Wrappers.query();
        if (name != null) {
            queryWrapper.like("employee_name", name);
        }

        if (phone != null) {
            queryWrapper.eq("employee_phone", phone);
        }

        if (email != null) {
            queryWrapper.eq("employee_email", email);
        }

        if (deptId != null) {
            queryWrapper.eq("dept_id", deptId);
        }
        if (status != null) {
            queryWrapper.eq("employee_status", status);
        }


        iPage = empMapper.selectPage(iPage, queryWrapper);

        List<Emp> empList = iPage.getRecords();
        EmpVo empVo;
        Dept dept;
        List<EmpVo> empVoList = new ArrayList<>();
        for (Emp emp : empList) {
            dept = deptMapper.selectById(emp.getDeptId());
            empVo = BeanHelper.copyProperties(emp, EmpVo.class);
            empVo.setArName(dept.getArName());
            empVo.setSStatus(dept.getSStatus());
            empVoList.add(empVo);
        }



        return empVoList;

    }

    @Override
    public void insert(EmpVo empVo) {

        Long deptId = empVo.getDeptId();
        Dept dept = deptMapper.selectById(deptId);
        if (dept.getId() != null) {
            Emp emp;
            emp = BeanHelper.copyProperties(empVo, Emp.class);
            emp.setDeptId(dept.getId());
            try {
                empMapper.insert(emp);

            } catch (Exception e) {
                e.printStackTrace();
                throw new CfException(ExceptionEnum.INSERT_OPERATION_FAIL);
            }
        }


    }

    @Override
    public EmpVo findById(Long id) {
        EmpVo empVo;
        Emp emp = empMapper.selectById(id);
        empVo = BeanHelper.copyProperties(emp, EmpVo.class);
        if (emp != null) {
            Dept dept = deptMapper.selectById(emp.getDeptId());
            empVo.setDeptId(dept.getId());
            empVo.setArName(dept.getArName());
            empVo.setSStatus(dept.getSStatus());
        }
        System.out.println(empVo);
        return empVo;
    }

    @Override
    public void updateDept(Emp emp) {
        try {
            empMapper.updateById(emp);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CfException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    @Override
    public void updateStatus(Long empId, Integer status) {
        Emp emp = empMapper.selectById(empId);
        emp.setEmployeeStatus(status);
        try {
            empMapper.updateById(emp);
        } catch (Exception e) {
            e.printStackTrace();
            throw new CfException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }


}
